package com.ruiysoft.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiysoft.entity.sys.SysShopPersonnelRelation;
import com.ruiysoft.mapper.sys.SysShopPersonnelRelationMapper;
import com.ruiysoft.service.sys.ISysShopPersonnelRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huazengguang
 * @since 2018-11-17
 */
@Service
public class SysShopPersonnelRelationServiceImpl extends ServiceImpl<SysShopPersonnelRelationMapper, SysShopPersonnelRelation> implements ISysShopPersonnelRelationService {

    @Resource
    SysShopPersonnelRelationMapper sysShopPersonnelRelationMapper;

    @Override
    public void insertIgnore(Long shopId, Long sharingId, Long personnelId, Integer identityType) {
        SysShopPersonnelRelation relation = new SysShopPersonnelRelation()
                .setPersonnelId(personnelId)
                .setShopId(shopId)
//                .setIdentityType(identityType)
                .setParentId(sharingId);
        sysShopPersonnelRelationMapper.insertIgnore(relation);

    }

    @Override
    public void insertIgnore(SysShopPersonnelRelation relation) {
        sysShopPersonnelRelationMapper.insertIgnore(relation);
    }

    @Override
    public SysShopPersonnelRelation selectByShopIdAndPersonnelId(Long shopId, Long personnelId) {
        return sysShopPersonnelRelationMapper.selectOne(new QueryWrapper<SysShopPersonnelRelation>().eq("shop_id", shopId).eq("personnel_id", personnelId));
    }

    @Override
    @Transactional
    public void updateShopPersonnelRelation(List<SysShopPersonnelRelation> relations) {
        relations.forEach(e -> sysShopPersonnelRelationMapper.updateById(e));
    }

    /**
     * 设置分销关系
     * @param shopId
     * @param personnelId
     * @param partnerId
     */
    @Override
    @Transactional
    public void setDistributionRelation(Long shopId, Long personnelId, Long partnerId) {
        SysShopPersonnelRelation relation = new SysShopPersonnelRelation().setPersonnelId(personnelId).setShopId(shopId).setPersonnelId(partnerId);/*.setIdentityType(shopkeeper)*/;
        relation=sysShopPersonnelRelationMapper.selectOne(new QueryWrapper<SysShopPersonnelRelation>().eq("shop_Id",shopId).eq("personnel_Id",personnelId));
        if(relation!=null && (relation.getParentId()==null || relation.getParentId()==0) && relation.getParentId()!=personnelId && personnelId.compareTo(partnerId)!=0){ //判断用户是否有上级,如果没有上级则设置上级,如果存在,则忽略
            relation.setParentId(partnerId);
            relation.setRelationTime(new Date());
            sysShopPersonnelRelationMapper.updateById(relation);
        }
    }

    //    @Override
//    public void saveOrUpdate(Long shopId, Long personnelId, Integer shopkeeper) {
//        SysShopPersonnelRelation relation = new SysShopPersonnelRelation().setPersonnelId(personnelId).setShopId(shopId)/*.setIdentityType(shopkeeper)*/;
//        sysShopPersonnelRelationMapper.saveOrUpdate(relation);
//    }
}
